package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionService;
import com.heima.feigns.behavior.BehaviorFeign;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.constants.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    @Autowired
    BehaviorFeign behaviorFeign;
    @Autowired
    KafkaTemplate kafkaTemplate;
    /**
     * 收藏与取消收藏接口,   远程调用查询行为实体
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //前端未传articleId
        if (dto==null || dto.getEntryId()==null|| dto.getOperation()==null
                ||dto.getOperation()<0 || dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查找行为实体不能为空
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null||user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"请登录后操作");
        }
        ApBehaviorEntry behaviorEntry = behaviorFeign
                .findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (behaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApCollection collection = getOne(
                Wrappers.<ApCollection>lambdaQuery()
                        .eq(ApCollection::getArticleId, dto.getEntryId())
                        .eq(ApCollection::getEntryId, behaviorEntry.getId())
        );
        //**** *************用户收藏-发送消息给实时计算
        if (dto.getOperation()==0){
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setAdd(1);
            mess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
            mess.setArticleId(dto.getEntryId());
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,
                    UUID.randomUUID().toString(), JSON.toJSONString(mess));
        }
        // ************************
        //0收藏
        if (dto.getOperation()==0){
            if (collection!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已收藏");
            }
            collection= ApCollection.builder()
                    .entryId(behaviorEntry.getId())
                    .articleId(dto.getEntryId())
                    .type(dto.getType())
                    .collectionTime(new Date())
                    .publishedTime(dto.getPublishedTime())
                    .build();
            save(collection);
        }
        //1取消
        if (dto.getOperation()==1){
            if (collection==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无此收藏");
            }
            removeById(collection.getId());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
